public class MyObjectPool {
	/**
	 * objects - array of objects
	 * ind[i] - indicates employment of objects[i] : if 0 - free, else 1 
	 */
	private Object[] objects;
	private int[] ind;
	private int size;
	public MyObjectPool(int startSize) {
		this.objects = new Object[startSize];
		this.ind = new int[startSize];
		for (int i = 0; i < startSize; i++) {
			ind[i] = 0;
		}
		this.size = 0;
	}
	public synchronized Object getObject() {
		for (int i = 0; i < objects.length; i++) {
			if ((ind[i] == 0)&&(objects[i] != null)) {
				ind[i] = 1;
				return objects[i];
			}
		}
		return null;
	}
	public synchronized void setObject(Object object) {
		if (object == null) 
			return;
		for (int i = 0; i < objects.length; i++) {
			if (objects[i] == object) {
				if (ind[i] == 1)
					ind[i] = 0;
				return;
			}
		}
		if (size < objects.length) {
			for (int i = 0; i < objects.length; i++) {
				if ((objects[i] == null)&&(ind[i] == 0)) {
					objects[i] = object;
					size++;
					return;
				}
			}
		}
		Object[] objectsNew = new Object[2*objects.length];
		int[] indNew = new int[2*ind.length];
		for (int i = 0; i < objects.length; i++) {
			objectsNew[i] = objects[i];
			indNew[i] = ind[i];
		}
		objectsNew[objects.length] = object;
		objects = objectsNew;
		ind = indNew;
	}
	public synchronized void clearPool() {
		for (int i = 0; i < objects.length; i++) {
			objects[i] = null;
			ind[i] = 0;
		}
	}
}
